import traceback

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from datetime import datetime

class BasePage():

    def __init__(self,driver):
        # self.driver = webdriver.Chrome()
        self.driver = driver

    # 查找定位元素
    def __get_element(self, locator, doc):
        print("开始查找元素%s"%str(locator))
        try:
            start_time = datetime.now()
            self.wait(locator)
            ele = self.driver.find_element(*locator)
            end_time = datetime.now()
            time = (end_time - start_time).seconds
            print("元素%s定位成功，用时%s"%(str(locator),time))
            return ele
        except Exception as e:
            print("查找%s元素"%str(locator))
            traceback.print_exc() # 打印报错信息
            self.get_screenshoot_as_file(doc)
		print(123)
        print(456)


    # 输入操作
    def input_context(self, locator, value, doc):
        ele = self.__get_element(locator, doc)
        ele.send_keys(value)

    # 点击操作
    def click_element(self,locator, doc):
        ele = self.__get_element(locator, doc)
        ele.click()


    def get_screenshoot_as_file(self, doc):
        """
        截图并保存
        :param doc: 模块名称_页面名称_操作名称
        :return:
        """
        self.driver.get_screenshot_as_file('../test_results/images/'+doc+'.png')

    def switch_into_frame(self, locator, doc):
        """
        切入frame框架中
        :param locator:
        :param doc:
        :return:
        """
        frame = self.__get_element(locator, doc)
        self.driver.switch_to.frame(frame)

    def switch_outo_frame(self):
        """
        从当前frame中切出
        :return:
        """
        self.driver.switch_to.default_content()

    def get_element_attribute(self, locator, doc, attribute):
        """
        获取元素的指定属性值
        :param locator: 元素定位方式
        :param doc: 截图图片名称
        :param attribute: 属性名称
        :return:
        """
        ele = self.__get_element(locator, doc)
        value = ele.get_attribute(attribute)
        return value

    def get_element_text(self, locator, doc):
        """
        获取元素的文本值
        :param locator: 元素定位方式
        :param doc: 截图图片名称
        :return:
        """
        ele = self.__get_element(locator, doc)
        text = ele.text
        return text

    def wait(self, locator, timeout=10, poll=0.5):
        """
        显示等待
        :param locator: 等待元素的定位方式
        :param timeout: 最长等待时间
        :param poll: 轮询时间
        :return:
        """
        try:
            start_time = datetime.now()
            WebDriverWait(self.driver, timeout,poll_frequency=poll).until(EC.visibility_of_element_located(locator))
            end_time = datetime.now()
            time = (end_time-start_time).seconds
            print("等待元素%s成功,耗时%s"%(str(locator), time))
        except Exception as e:
            print("等待元素%s失败"%str(locator))
            traceback.print_exc()

    def execute_js(self, js, ele=None):
        self.driver.execute_script(js,ele)

    def remove_element_attribute(self, locator,doc,attribute):
        """
        通过selenium 定位元素，然后通过javascript移除指定的元素属性
        :param locator:
        :param doc:
        :param attribute: 需要移除的元素属性名称
        :return:
        """
        ele = self.__get_element(locator, doc)
        js = 'arguments[0].remove("%s")'%attribute
        self.execute_js(js, ele)

    def switch_to_window(self, index=-1):
        """
        切换窗口到指定的 窗口
        :param index: 窗口列表中窗口的下标
        :return:
        """
        self.driver.switch_to.window(self.driver.window_handles[index])

    def switch_to_alert(self, action='accept', value=None):
        """
        切换到alert弹窗并进行操作
        :param action: 操作的类型 accept确认/确定  dismisss 取消/关闭
        :param value: 输入时，需要输入的值
        :return:
        """
        alert = self.driver.switch_to.alert
        if action == 'accept':
            alert.accept()
        elif action == 'dismiss':
            alert.dismiss()
        else:
            alert.send_keys(value)
            alert.accept()

    def select_action(self, locator, doc, index=None,value=None, visible_text=None):
        """
        下拉框操作
        :param locator:
        :param doc:
        :param index: 通过下标进行选择
        :param value: 通过value属性值进行选择
        :param visible_text: 通过文本值进行选择
        :return:
        """
        ele = self.__get_element(locator, doc)
        select = Select(ele)
        if index is not None:
            select.select_by_index(index)

        if value is not None:
            select.select_by_value(value)

        if visible_text is not None:
            select.select_by_visible_text(visible_text)

